package com.wenhai.leetcode.tree;

/**
 * https://leetcode-cn.com/problems/invert-binary-tree/
 * 翻转二叉树
 *
 * @author wenhai
 * @date   2021/1/19
 */
public class _226翻转二叉树 {


    public static void main(String[] args) {
        TreeNode root = new TreeNode(10);
        TreeNode right = new TreeNode(9);
        right.left = new TreeNode(4);
        right.right = new TreeNode(6);
        root.right = right;
        TreeNode left = new TreeNode(5);
        left.left = new TreeNode(3);
        left.right = new TreeNode(2);
        root.left =left;
        root = invertTree(root);
        System.out.println(root);
    }


    public static TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return root;
        }
        TreeNode tmp = root.right;
        root.right = root.left;
        root.left = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }
}
